package scau.filesystem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import scau.filesystem.bean.FileTree;

public class FileAbsolutePathImp {
	private FileTree rootNode;

	public FileAbsolutePathImp(FileTree rootNode) {
		super();
		this.rootNode = rootNode;
	}
	

	
	public List<String> getPath(FileTree fileTree){
		Map<String, FileTree> map = fileTree.getChildTree();
		List<String> path = new ArrayList<>();
		for(String key : map.keySet()){
			FileTree ft = map.get(key);
			if(ft.getChildTree() == null || ft.getChildTree().keySet().size() == 0){
				if(ft.isType())
					path.add(fileTree.getFilename() + "\\" + ft.getFilename() + "\\");
				else
					path.add(fileTree.getFilename() + "\\" + ft.getFilename());
			}else{
				List<String> childPath = getPath(ft);
				for(String next : childPath){
					path.add(fileTree.getFilename() + "\\" + next);
				}
			}
		}
		return path;
	}
	
	public List<String> getPaths(FileTree fileTree){
		List<String> list = getPath(fileTree);
		for(int i=0; i<list.size();i++){
			list.set(i, list.get(i).substring(1));
		}
		return list;
	}
	
	public static void main(String[] args) {
		FileTree root=new FileTree();
		root.clear();
		String filename = null;
		Map<String,FileTree> fileTree=new HashMap<String, FileTree>();
		for(int i=0;i<=10;i++){
			filename=""+i;
			FileTree tree=new FileTree();
			tree.setFilename(filename);
			tree.setType(true);
			fileTree.put(filename, tree);
			Map<String, FileTree> fileTree2 = new HashMap<>();
			for(int j=0;j<=10;j++){
				String filename1=""+j;
				FileTree tree1=new FileTree();
				tree1.setFilename(filename1);
				tree1.setType(false);
				fileTree2.put(filename1, tree1);
			}
			tree.setChildTree(fileTree2);
		}
		root.setChildTree(fileTree);
		FileAbsolutePathImp imp=new FileAbsolutePathImp(root);
//		ArrayList<FilePathInfo> arr=imp.convertToAboslutePath(root, 0);

		List<String> list = imp.getPaths(root);
		for(String p : list){
			System.out.println(p);
		}
	} 
}
